'use strict';

// Declare app level module which depends on views, and components

//.controller('ButtonsCtrl', function($scope) {
//  $scope.singleModel = 1;
//  $scope.radioModel = 'Middle';
//  $scope.checkModel = {
//    left: false,
//    middle: true,
//    right: false
//  };
//  $scope.checkResults = [];
//  $scope.$watchCollection('checkModel', function () {
//    $scope.checkResults = [];
//    angular.forEach($scope.checkModel, function (value, key) {
//      if (value) {
//        $scope.checkResults.push(key);
//      }
//    });
//  });
//});

window.app=angular.module('myApp', ['ngRoute',
    //'monitor.schpacket',
    'textAngular',
    'datePicker',
    'angular.echart',
    'angular.select2',
    'ui.bootstrap'], ['$routeProvider','$httpProvider',function ($routeProvider,$httpProvider) {//MyModule是你自己的app名称
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';


    var param = function (obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

        for (name in obj) {
            value = obj[name];

            if (value instanceof Array) {
                for (i = 0; i < value.length(); i++) {
                    subValue = value[i];
                    fullSubName = name + '[' + i + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }

            } else if (value instanceof Object) {
                for (subName in value) {
                    subValue = value[subName];
                    fullSubName = name + '[' + subName + ']';
                    innerObj = {};
                    innerObj[fullSubName] = subValue;
                    query += param(innerObj) + '&';
                }
            }
            else if (value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }

        return query.length ? query.substr(0, query.length - 1) : query;
    };

// Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function (data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];

    $routeProvider.otherwise({redirectTo: '/'});
    $httpProvider.interceptors.push('timestampMarker');

}]).factory('timestampMarker', ["$rootScope", function ($rootScope) {
    $rootScope.loading_text="努力加载中..."
    $rootScope.mask=true;
    var timestampMarker = {
        request: function (config) {
            if ($rootScope.mask) {
                $rootScope.loading = true;
            }
                config.requestTimestamp = new Date().getTime();
              //  console.log("request")
                return config;

        },
        response: function (response) {
             $rootScope.loading = false;
            response.config.responseTimestamp = new Date().getTime();
           // console.log("response")
            return response;
        }
    };
    return timestampMarker;
}]);
app.config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$routeProvider', function($controllerProvider, $compileProvider, $filterProvider, $provide,$routeProvider) {
    app.register = {
        controller: $controllerProvider.register,
        directive: $compileProvider.directive,
        filter: $filterProvider.register,
        factory: $provide.factory,
        service: $provide.service,
        provide:$provide
    };

    $routeProvider.when('/:model/:fun', {
        templateUrl: function(rd) {
            console.log(rd.model+'/' + rd.fun + '/'+rd.fun+'.html')
            return rd.model+'/' + rd.fun + '/'+rd.fun+'.html';
        },
        resolve: {
            load: ["$q", "$route", "$rootScope", function ($q, $route, $rootScope) {
                var deferred = $q.defer();
                var controljs= $route.current.params.model+'/' + $route.current.params.fun + '/'+$route.current.params.fun+'.js';

                //if (!!js ) {
                //    if (angular.isArray(js)) {
                //        js.push(controljs);
                //    } else if (angular.isString(js)) {
                //        js=[js,controljs]
                //    } else {
                //        js=[controljs]
                //    }
                //} else {
                //    js=[controljs]
                //}
                console.log("controljs:"+controljs)
                var dependencies = controljs;
                //if (Array.isArray(dependencies)) {
                //    for (var i = 0; i < dependencies.length; i++) {
                //        dependencies[i] += "?v=" + v;
                //    }
                //} else {
                //    dependencies += "?v=" + v;//v是版本号
                //}
                if (dependencies) {
                    $script(dependencies, function () {
                        $rootScope.$apply(function () {
                            deferred.resolve();
                        });
                    });
                }

                return deferred.promise;
            }]
        }
    });

}])

